<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图片懒加载</title>
    <style>
        img{
            width:300px;
            height:auto;
        }
    </style>
</head>
<body>
<div id="div">
    <div id="one">
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
        <img class="img" data-url="psbQXFQ7FQY.jpg" src="psb0YPHO3XY.jpg" alt="love"/>
    </div>
</div>
<script>
//    var num = document.getElementsByTagName('img').length;
//    var img = document.getElementsByTagName("img");
//    var n = 0; //存储图片加载到的位置，避免每次都从第一张图片开始遍历
//    lazyload(); //页面载入完毕加载可是区域内的图片
//    window.onscroll = lazyload;
//    function lazyload() { //监听页面滚动事件
//        var seeHeight = document.documentElement.clientHeight; //可见区域高度
//        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; //滚动条距离顶部高度
//        for (var i = n; i < num; i++) {
//            if (img[i].offsetTop < seeHeight + scrollTop) {
//                if (img[i].getAttribute("src") == "psb0YPHO3XY.jpg") {
//                    img[i].src = img[i].getAttribute("data-url");
//                }
//                n = i + 1;
//            }
//        }
//    }
    var aImg=document.getElementsByTagName("img");
    var len=aImg.length;
    //lazy();
    window.onscroll=setTimeout(lazy,1000);
    function lazy(){
        var iB=document.documentElement.clientHeight;
        var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
        var url='';
        for(var i=0;i<len;i++){
            console.log(scrollTop);
            console.log(iB);
            if(!aImg[i].flag&&aImg[i].offsetTop<=scrollTop+iB){
                url=aImg[i].getAttribute("data-url");
                aImg[i].flag=true;
                aImg[i].src=url;
            }
        }
    }


</script>
</body>
</html>